﻿using UnityEngine;
using System.Collections;

public class UIAutoSize : MonoBehaviour {

    public UISize mySize { set; get; }

    public TweenPosition framePanel_Top;
    public TweenPosition framePanel_Bottom;
    private static UIAutoSize instance;
    
    public Vector2 originSize;
    public Vector2 manualSize;
    public UIRoot root;
    public bool isShow;
    public static UIAutoSize Instance
    {
        get
        {
            return instance;
        }
    }
    public enum UISize
    {
        Size_16_9,
        Size_16_10,
        Size_3_2,
        Size_4_3,
    }
    
    // Use this for initialization
    void Awake () {
        instance = this;
        mySize = JudgeSize();
        if (root == null)
            root = GetComponent<UIRoot>();
        if (mySize != UISize.Size_16_9)
            root.manualWidth = 1080;
        else
            root.manualWidth = 1136;
        if (mySize == UISize.Size_3_2 || mySize == UISize.Size_4_3)
            framePanel_Top.transform.parent.gameObject.SetActive(true);
        else
            framePanel_Top.transform.parent.gameObject.SetActive(false);

        double sWidth = Screen.width;
        double sHeight = Screen.height;
        originSize = new Vector2(Screen.width,Screen.height);
        double targetWidth = 1280;
        double targetHeght;
#if UNITY_ANDROID
        if (sWidth >= 1300)
        {
            targetHeght = targetWidth * sHeight / sWidth;
            Screen.SetResolution((int)targetWidth, (int)targetHeght, true);//根据宽度重设分辨率
        }
#endif
        manualSize = new Vector2(Screen.width, Screen.height);

        //
        if(originSize.x < originSize.y)
        {
            originSize = new Vector2(originSize.y, originSize.x);
        }

        //
        Debug.Log("screen size type: " + mySize);
        Debug.Log("screen size is : " + manualSize.x + " " + manualSize.y);
    }

    //判断屏幕最接近哪种尺寸
    UISize JudgeSize()
    {
        float[] screenSize = { 16f / 9f, 16f / 10f, 3f / 2f, 4f / 3f };
        float nowSize = (float)Screen.width / (float)Screen.height;
        if(nowSize < 1)
        {
            nowSize = 1f / nowSize;
        }
        float[] size = new float[4];
        
        for (int i = 0; i < screenSize.Length; i++)
        {
            size[i] = Mathf.Abs(nowSize - screenSize[i]);
        }

        float min = size[0];
        int index = 0;
        for (int i = 1; i < size.Length; i++)
        {
            if(size[i] <= min)
            {
                min = size[i];
                index = i;
            }
        }
        return (UISize)(index);
    }

    //是否显示上下遮挡的UI
    public void ShowMaskUI(bool ishow)
    {
        if (framePanel_Top == null || framePanel_Bottom == null)
            return;
        if (mySize == UISize.Size_16_9 || mySize == UISize.Size_16_10)
            return;
        //framePanel_Top.ResetToBeginning();
        //framePanel_Bottom.ResetToBeginning();
        if (ishow)
        {
            framePanel_Top.PlayForward();
            framePanel_Bottom.PlayForward();
        }
        else
        {
            framePanel_Top.PlayReverse();
            framePanel_Bottom.PlayReverse();
        }
        this.isShow = ishow;
    }
}
